package com.lanyou.esb.cook.proxy.redis.persistent.base;

import java.util.ArrayList;
import java.util.List;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

import com.lanyou.esb.cook.proxy.common.ParamFactory;
import com.lanyou.esb.cook.proxy.exception.ParamSetRuntimeException;
import com.lanyou.esb.cook.proxy.redis.persistent.PersistentRedis;
import com.lanyou.esb.cook.proxy.service.ParamSetService;
import com.lanyou.esb.cook.proxy.service.Service;

/**
 * 持久化REDIS数据基类
 * 
 * @author Davey.wu
 */
public abstract class BasePersistentRedis<T> implements PersistentRedis<T>, Job {
	private Scheduler scheduler;
	private String cron = "0/10 * * * * ?";
	private String name;
	private String group;
	private Class jobClass;
	/** 批量提交数目 **/
	private int batchAmount = 500;
	/** 批量读取的数量 **/
	private int batchReadSize = 500;
	/** 需要保存的对象列表 **/
	//protected List<T> list = new ArrayList<>(batchAmount);

	/** 参数配置服务接口 **/
	private ParamSetService paramSetService;

	/** 服务接口 **/
	private Service service;

	@Override
	public void init() throws Exception {
		// 初始化参数
		//initParamSet();
		System.out.println("----------------persistent " + name
				+ " redis data init");
		try {
			scheduler = new StdSchedulerFactory().getScheduler();
			// 创建LzstoneTimeTask的定时任务
			// JobDetail jobDetail = new JobDetailImpl(name, group, jobClass);
			JobDetail jobDetail = JobBuilder.newJob(jobClass)
					.withIdentity(name, group).build();
			JobDataMap jobMap = jobDetail.getJobDataMap();
			jobMap.put("proxy", this);
			// 目标 创建任务计划
			CronTrigger trigger = new CronTriggerImpl(name, group, cron);
			// CronTrigger trigger =
			// TriggerBuilder.newTrigger().withIdentity(name,group).
			scheduler = new StdSchedulerFactory().getScheduler();
			scheduler.scheduleJob(jobDetail, trigger);
			scheduler.start();
		} catch (Exception e) {
			System.out.println("----------持久化" + name + "信息线程启动失败！");
			throw e;
		}
	}

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		// erase();
		PersistentRedis persistentRedis = (PersistentRedis) context
				.getJobDetail().getJobDataMap().get("proxy");
		try {
			persistentRedis.persistent();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("---------------persistent " + name
				+ " redis data destroy");
		if (scheduler == null) {
			return;
		}
		try {
			scheduler.shutdown();
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * @throws ParamSetException
	 * @throws ParamSetRuntimeException
	 * 
	 */
	private void initParamSet() {
		if (ParamFactory.haveParam()) {
			return;
		}
		// 加载参数配置
		paramSetService.init();
	}

	public int getBatchAmount() {
		return batchAmount;
	}

	public void setBatchAmount(int batchAmount) {
		this.batchAmount = batchAmount;
	}

	public String getCron() {
		return cron;
	}

	public void setCron(String cron) {
		this.cron = cron;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public Class getJobClass() {
		return jobClass;
	}

	public void setJobClass(Class jobClass) {
		this.jobClass = jobClass;
	}

	public ParamSetService getParamSetService() {
		return paramSetService;
	}

	public void setParamSetService(ParamSetService paramSetService) {
		this.paramSetService = paramSetService;
	}

	@Override
	public Service getService() {
		return service;
	}

	public void setService(Service service) {
		this.service = service;
	}

	public int getBatchReadSize() {
		return batchReadSize;
	}

	public void setBatchReadSize(int batchReadSize) {
		this.batchReadSize = batchReadSize;
	}
}
